package cn.devzyh.service.impl;

import cn.devzyh.dao.ResourceMapper;
import cn.devzyh.domain.Resource;
import cn.devzyh.domain.ResourceVo;
import cn.devzyh.service.ResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public PageInfo<Resource> findByConditionAndPage(ResourceVo resourceVo) {

        PageHelper.startPage(resourceVo.getCurrentPage(), resourceVo.getPageSize());
        List<Resource> resources = resourceMapper.findByCondition(resourceVo);
        PageInfo<Resource> pageInfo = new PageInfo<>(resources);

        return pageInfo;
    }

    @Override
    public void saveResource(Resource resource) {

        Date now = new Date();
        resource.setCreatedTime(now);
        resource.setUpdatedTime(now);
        resource.setCreatedBy("system");
        resource.setUpdatedBy("system");

        resourceMapper.saveResource(resource);
    }

    @Override
    public void updateResource(Resource resource) {

        resource.setUpdatedTime(new Date());
        resource.setUpdatedBy("system");

        resourceMapper.updateResource(resource);
    }

    @Override
    public void deleteResource(Integer id) {

        resourceMapper.deleteResource(id);
    }
}
